<?php
/**
 * Created by PhpStorm.
 * User: shenyl
 * Date: 2021/1/12
 * Time: 15:56
 */
namespace app\api\model\user;

use app\api\controller\pay\Alipay;
use app\api\controller\pay\Wxpay;
use think\Model;
use think\Db;
class OrderpayModel extends Model{
    /**
     * 退款 v2.0
     */
    public function refund($pay_log_id, $refund_fee, $pay_method, $transaction_id, $out_refund_no){
        if($pay_method ==1){
            // $wxPay = new Wxpay();
            // $result = $wxPay->refund($transaction_id,$out_refund_no,$refund_fee,$refund_fee);

            $result['return_code'] = 'SUCCESS';
            $result['result_code'] = 'SUCCESS';
            $result['refund_id'] = '11112222';
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
                db("order_pay_log")->where(['id'=>$pay_log_id])->update([
                    'refund_id'=>$result['refund_id'],
                ]);
            }
            else{
                return false;
            }
        }
        elseif($pay_method ==2){
            // $aliPay = new Alipay();
            // $result = $aliPay->refund($out_refund_no, $transaction_id, sprintf("%.2f", $refund_fee/100), '交易取消'); 
    
            $result['code'] = 10000;
            $result['trade_no'] = 2222222222222222;
            if($result['code'] == 10000){
                db("order_pay_log")->where(['id'=>$pay_log_id])->update([
                    'refund_id'=>$result['trade_no'],
                    'refund_status'=>1,
                    'refund_time_end'=>date("Y-m-d H:i:s"),
                ]);
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
        return true;
    }
}